/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormAberturaChamados.java
 *
 * Created on 06/12/2011, 01:13:30
 */

package sistemachamados.view;

import classes.CategoriaChamado;
import classes.Chamado;
import classes.Usuario;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Cleber Lopes
 */
public class FormAberturaChamados extends javax.swing.JInternalFrame {

    private Chamado chamado;
    private Usuario usuarioLogado;
    /** Creates new form FormAberturaChamados */
    public FormAberturaChamados(Usuario usuarioLogado) {
        initComponents();
        chamado = new Chamado();
        this.usuarioLogado = usuarioLogado;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtIdChamado = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtDataAbertura = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        cbxCategoriaChamado = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescricaoProblema = new javax.swing.JTextArea();
        btnCancelar = new javax.swing.JButton();
        btnEnviar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();

        setBorder(null);
        setClosable(true);
        setFrameIcon(null);
        setInheritsPopupMenu(true);
        setMinimumSize(new java.awt.Dimension(990, 470));
        setPreferredSize(new java.awt.Dimension(990, 470));
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "INFORMAÇÕES DO CHAMADO", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Showcard Gothic", 0, 16))); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(FormAberturaChamados.class);
        jPanel1.setFont(resourceMap.getFont("jPanel1.font")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setOpaque(false);

        jLabel1.setFont(new java.awt.Font("Gautami", 3, 16));
        jLabel1.setText("ID do Chamado");
        jLabel1.setName("jLabel1"); // NOI18N

        txtIdChamado.setEditable(false);
        txtIdChamado.setFont(new java.awt.Font("Tahoma", 0, 14));
        txtIdChamado.setName("txtIdChamado"); // NOI18N
        txtIdChamado.setOpaque(false);

        jLabel2.setFont(new java.awt.Font("Gautami", 3, 16));
        jLabel2.setText("Data de Abertura");
        jLabel2.setName("jLabel2"); // NOI18N

        txtDataAbertura.setEditable(false);
        txtDataAbertura.setFont(new java.awt.Font("Tahoma", 0, 14));
        txtDataAbertura.setName("txtDataAbertura"); // NOI18N
        txtDataAbertura.setOpaque(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtIdChamado, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDataAbertura, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(352, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(txtIdChamado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDataAbertura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 20, 947, 73);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setOpaque(false);
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setFont(new java.awt.Font("Gautami", 3, 16));
        jLabel3.setText("Categoria do Chamado");
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        cbxCategoriaChamado.setFont(new java.awt.Font("Century Gothic", 0, 14));
        cbxCategoriaChamado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione uma categoria de Chamado..." }));
        cbxCategoriaChamado.setName("cbxCategoriaChamado"); // NOI18N
        jPanel2.add(cbxCategoriaChamado, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 20, 311, -1));

        jLabel4.setFont(new java.awt.Font("Gautami", 3, 16));
        jLabel4.setText("Descrição do Problema");
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, 20));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtDescricaoProblema.setColumns(20);
        txtDescricaoProblema.setFont(new java.awt.Font("Kartika", 0, 16));
        txtDescricaoProblema.setRows(5);
        txtDescricaoProblema.setToolTipText("Digite maiores informações do problema"); // NOI18N
        txtDescricaoProblema.setName("txtDescricaoProblema"); // NOI18N
        jScrollPane1.setViewportView(txtDescricaoProblema);

        jPanel2.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, 900, 160));

        btnCancelar.setFont(new java.awt.Font("Berlin Sans FB Demi", 0, 14));
        btnCancelar.setText("Cancelar");
        btnCancelar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        jPanel2.add(btnCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 280, 110, -1));

        btnEnviar.setFont(new java.awt.Font("Berlin Sans FB Demi", 0, 14));
        btnEnviar.setText("Enviar");
        btnEnviar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        btnEnviar.setName("btnEnviar"); // NOI18N
        btnEnviar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnEnviarMouseClicked(evt);
            }
        });
        jPanel2.add(btnEnviar, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 280, 110, -1));

        getContentPane().add(jPanel2);
        jPanel2.setBounds(10, 99, 947, 325);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemachamados/view/resources/FORM PRINCIPAL (FUNDOPANEL).png"))); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        getContentPane().add(jLabel5);
        jLabel5.setBounds(0, 0, 970, 460);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        chamado = chamado.getLastIdChamado();
        txtIdChamado.setText(String.valueOf(chamado.getId()));
        SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        txtDataAbertura.setText((formatador.format(date)));
        CategoriaChamado categoria = new CategoriaChamado();
        ArrayList<CategoriaChamado> listCategoriasChamados = categoria.selecionar();
        cbxCategoriaChamado.setModel(new DefaultComboBoxModel(listCategoriasChamados.toArray()));
        txtDescricaoProblema.setText("");
    }//GEN-LAST:event_formComponentShown

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnEnviarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEnviarMouseClicked
        chamado.setDataAbertura(new Date());
        chamado.setCategoria((CategoriaChamado)cbxCategoriaChamado.getSelectedItem());
        chamado.setId(Integer.parseInt(txtIdChamado.getText()));
        chamado.setDescrProblema(txtDescricaoProblema.getText());
        if(chamado.salvar(usuarioLogado))
        {
            JOptionPane.showMessageDialog(this, "Seu chamado foi aberto com sucesso. Aguarde atendimento.",
                                                "Sucesso!",
                                                JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        }
        else
        {
            JOptionPane.showMessageDialog(this, "Houve um erro ao abrir o chamado. Tente novamente.",
                                                "Erro!",
                                                JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnEnviarMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEnviar;
    private javax.swing.JComboBox cbxCategoriaChamado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtDataAbertura;
    private javax.swing.JTextArea txtDescricaoProblema;
    private javax.swing.JTextField txtIdChamado;
    // End of variables declaration//GEN-END:variables

}
